package com.takanafu.haru.code{
	import flash.utils.Dictionary;
	
	public class CodeObject extends CodeBase{
		private var slots:Dictionary;
		private var proto:CodeObject = null;
		private var secondProto:CodeObject = null;
		
		public function CodeObject(param:*,object:*=null){
			if(param is Dictionary){
				this.slots = param as Dictionary;
			}else if(param is CodeObject){
				if(object != null && object is CodeObject){
					this.secondProto = param as CodeObject;
					this.proto = object as CodeObject;
				}else{
					this.proto = param as CodeObject;
				}
				this.slots = new Dictionary();
			}
		}
		
		override public function toString():String{
			return "<object>";
		}
		public function hasSymbol(sym:CodeSymbol):Boolean{
			if(this.slots.hasOwnProperty(sym) == true){
				return true;
			}
			if((this.proto != null) && this.proto.hasSymbol(sym)){
				return true;
			}
			if((this.secondProto != null) && this.secondProto.hasSymbol(sym)){
				return true;
			}
			return false;
		}
		public function getSymbolValue(sym:CodeSymbol):CodeBase{
			var code:CodeBase = null;
			
			if(this.slots.hasOwnProperty(sym) == true){
				code = this.slots[sym] as CodeBase;
			}else if((this.proto != null) && this.proto.hasSymbol(sym)){
				code = this.proto.getSymbolValue(sym);
			}else if((this.secondProto != null) && this.secondProto.hasSymbol(sym)){
				code = this.secondProto.getSymbolValue(sym);
			}
			return code;
		}
		public function setSymbolValue(sym:CodeSymbol,code:CodeBase):void{
			if(this.slots.hasOwnProperty(sym) == true){
				this.slots[sym] = code;
			}else if((this.proto != null) && this.proto.hasSymbol(sym)){
				this.slots[sym] = code;
			}else if((this.secondProto != null) && this.secondProto.hasSymbol(sym)){
				this.slots[sym] = code;
			}else{
				throw new Error("対応するフィールドがありません。");
			}
		}
	}
}